/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    TreeNode* Convert(TreeNode* pRootOfTree)
    {
        if(pRootOfTree == nullptr)
        	return nullptr;
        TreeNode *head = new TreeNode(0);
        TreeNode *pLastNode = head;
        convertCore(pRootOfTree,pLastNode);
        head = head->right;
        head->left = nullptr;
        return head;
    }

    void convertCore(TreeNode *root,TreeNode *&pLastNode)
    {
    	if(root->left)
    		convertCore(root->left,pLastNode);
    	pLastNode->right = root;
    	root->left = pLastNode;
    	pLastNode = root;
    	if(root->right)
    		convertCore(root->right,pLastNode);
    }
};